﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;

public partial class Sendmail : System.Web.UI.UserControl
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    public void sets(string mail ,RoleEnum FromRole, RoleEnum ToRole , string companyid,string Customfields)
    {
      TextBox2.Text = mail;
      CompanyIDtxt.Text = companyid.ToString();
      Toroletxt.Text = ((int)ToRole).ToString();
      FromRoletxt.Text = ((int)FromRole).ToString();
      Customerfieldstxt.Text = Customfields;
    
    }
    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {


        HttpFileCollection postedfile = Request.Files;
        if (postedfile[0].ContentLength > 5196000)
        {

            return;

        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        if (TextBox2.Text.Contains("@"))
        {
            if (AJAXUploader1.PostedFiles.Length > 0)
            {
                //   DB.sendingmail(TextBox2.Text, subject.Text, Editor1.Content, AsyncFileUpload1.FileContent, AsyncFileUpload1.FileName);
                sendwithattach(TextBox2.Text, subject.Text);

            }
            else
            {
                DB.sendingmail(TextBox2.Text, subject.Text, Editor1.Content);
            }
            //string[] mails = TextBox2.Text.Split(',');
            //foreach (string dd in mails)
            //{
            //    if (AJAXUploader1.PostedFiles.Length > 0)
            //    {


            //        sendwithattach(dd, subject.Text);
            //    }
            //    //if (AsyncFileUpload1.HasFile)
            //    //{
            //    //    DB.sendingmail(dd, subject.Text, Editor1.Content, AsyncFileUpload1.FileContent, AsyncFileUpload1.FileName);

            //    //}
            //    else
            //    {
            //        DB.sendingmail(dd, subject.Text, Editor1.Content); //TextBox1.Text);
            //    }
            //}
        }

        RoleEnum FromRole =(RoleEnum) int.Parse(FromRoletxt.Text);
        RoleEnum ToRole = (RoleEnum) int.Parse(Toroletxt.Text);
        string Companyid = this.CompanyIDtxt.Text;
        if (FromRole==RoleEnum.Admin &ToRole == RoleEnum.Organization & !string.IsNullOrEmpty(Companyid))
        {

            List<string> fields = new List<string>();
            ArrayList values = new ArrayList();
            List<string> types = new List<string>();
            //////////////////////////////////////////////
            fields.Add("title");
            types.Add("string");
            values.Add(subject.Text);
            ///////////////////////
            /////////////////////////////
            fields.Add("message");
            types.Add("string");
            values.Add(Editor1.Content);
            /////////////////////////////
            /////////////
            fields.Add("company_id");
            types.Add("int");
            values.Add(Companyid);
           
            int ff = (int)DB.INSERT("system_messages", fields, types, values);
        }
        if(ToRole == RoleEnum.Admin )
        {
            List<string> fields = new List<string>();
            ArrayList values = new ArrayList();
            List<string> types = new List<string>();
            //////////////////////////////////////////////
            fields.Add("title");
            types.Add("string");
            values.Add(subject.Text);
            ///////////////////////
            /////////////////////////////
            fields.Add("message");
            types.Add("string");
            values.Add(Editor1.Content);
            /////////////////////////////
            /////////////
            fields.Add("company_id");
            types.Add("int");
            values.Add(Session["company_id"].ToString());
            ///////////////////////
            /////////////
            fields.Add(TextBox2.Text.Split(',')[1]);
            types.Add("int");
            values.Add(TextBox2.Text.Split(',')[0]);
            ///////////////////////
            if (AJAXUploader1.PostedFiles.Length > 0)
            {
                string vv = AJAXUploader1.PostedFiles[0].FinalFileName;
                /////////////////////////////
          
                fields.Add("attachfile");
                types.Add("string");
                values.Add(vv);
                /////////////////////////////
           
            }
            if (!string.IsNullOrEmpty(Customerfieldstxt.Text))
            { 
                List<string> pars = new List<string>();
                if (Customerfieldstxt.Text.Contains(','))
                {
                    pars.AddRange(Customerfieldstxt.Text.Split(','));
                }
                else
                {
                    pars.Add(Customerfieldstxt.Text);
                }
                foreach (string s in pars)
                {
                    fields.Add(s.Split(':')[0]);
                    types.Add(s.Split(':')[1]);
                    values.Add(s.Split(':')[2]);

                }
            }
            /////////////
            int ff = (int)DB.INSERT("messages", fields, types, values);
        }
        

    }
    public void sendwithattach(string too, string subjects)
    {
        EO.Web.AJAXPostedFile[] files = AJAXUploader1.PostedFiles;
        string[] names = new string[files.Length];
        for (int i = 0; i < files.Length; i++)
        {


            names[i] = files[i].TempFileName + "," + files[i].FullClientFileName;//System.IO.Path.GetFileName(files[i].TempFileName); ////Server.MapPath("~/temp/Example.xml") + 
            // Server.MapPath("~\temp\\") + files[i].TempFileName;

        }
        DB.sendingmail(too, subjects, Editor1.Content, names);
        AJAXUploader1.ClearPostedFiles();


    }


}
